package mo.tags.backtrack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class L90 {

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> combination = new ArrayList<>();
        Arrays.sort(nums);
        dfs(res, combination, 0, nums);
        return res;
    }

    private void dfs(List<List<Integer>> res, List<Integer> combination, int idx, int[] nums) {
        res.add(new ArrayList<>(combination));
        for (int i = idx; i < nums.length; i++) {
            if (i > idx && nums[i] == nums[i-1]) {
                continue;
            }
            combination.add(nums[i]);
            dfs(res, combination, i+1, nums);
            combination.remove(combination.size()-1);
        }
    }

    public static void main(String[] args) {
        L90 test = new L90();
        System.out.println(test.subsetsWithDup(new int[]{4,4,4,1,4}));
    }
}
